//
//  EmojiView.m
//  YunShouHu
//
//  Created by WangQiang on 16/6/22.
//  Copyright © 2016年 WangQiang. All rights reserved.
//

#import "EmojiView.h"
#import "EmojiCollectionViewCell.h"
#import "EmojiEmotion.h"
@interface EmojiView()<UICollectionViewDelegate,UICollectionViewDataSource>{
    UIPageControl *mPageControl;
}
@property (weak ,nonatomic) UICollectionView *faceCollection;
@property (strong ,nonatomic) NSArray *data;
@property (assign ,nonatomic) NSInteger pageCount;
@property (assign ,nonatomic) NSInteger page;

@end
@implementation EmojiView
static NSString *const ID = @"collectionViewCell";
-(instancetype)init{
    if(self = [super init]){
        self.backgroundColor = [UIColor whiteColor];
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        
        layout.minimumLineSpacing = 0.0;
        layout.minimumInteritemSpacing = 0.0;
//        layout.sectionInset = UIEdgeInsetsMake(10.0, 5.0, 0, 5.0);
        layout.itemSize = CGSizeMake(APP_WIGHT, 100);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        UICollectionView * faceCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, APP_WIGHT, 0) collectionViewLayout:layout];
        faceCollection.backgroundColor = [UIColor lightGrayColor];
        faceCollection.delegate = self;
        faceCollection.dataSource = self;
        [faceCollection registerClass:[EmojiCollectionViewCell class] forCellWithReuseIdentifier:ID];
        faceCollection.showsVerticalScrollIndicator = NO;
        faceCollection.showsHorizontalScrollIndicator = NO;
        
        faceCollection.pagingEnabled = YES;
        faceCollection.translatesAutoresizingMaskIntoConstraints = NO;
        self.faceCollection = faceCollection;
        [self addSubview:faceCollection];
        NSMutableArray *emojis  = [NSMutableArray arrayWithArray:[EmojiEmotion allEmojiEmotion]];
        //每行9个 一共三行
        NSInteger pageCount = 9*3;//3列
        NSInteger page = ceilf(emojis.count/(pageCount*1.0));
        page = ceilf((emojis.count+page)/(pageCount*1.0));//防止页数过多 加元素溢页
        self.page = page;
        self.pageCount = pageCount;
        for (int i = 0; i < page; i ++) {
            
            NSInteger index = (pageCount-1)*(i +1)+i;
            if(index > emojis.count - 1){
               [emojis addObject:[EmojiEmotion deleteEmoji]];
            }else{
              [emojis insertObject:[EmojiEmotion deleteEmoji] atIndex:index];
            }
        }
        self.data = emojis;
       
        UIPageControl *pageControl = [[UIPageControl alloc] init];
        pageControl.translatesAutoresizingMaskIntoConstraints = NO;
        pageControl.numberOfPages = page;
        pageControl.currentPage = 0;
        pageControl.backgroundColor = [UIColor lightGrayColor];
        mPageControl = pageControl;
//        pageControl.backgroundColor = [UIColor redColor];
        pageControl.pageIndicatorTintColor = TITLE3;
        pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
        [self addSubview:pageControl];
        UIButton *send = [[UIButton alloc] init];
        [send setTitle:NSLocalizedString(@"发送", nil) forState:UIControlStateNormal];
        send.backgroundColor = [UIColor blueColor];
        send.translatesAutoresizingMaskIntoConstraints = NO;
        [send addTarget:self action:@selector(sendContentAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:send];
        
        NSDictionary *views = NSDictionaryOfVariableBindings(faceCollection,pageControl,send);
        NSDictionary *metrics = @{@"paddingV":@0,@"pageControlH":@10,@"sendH":@35,@"sendW":@50};
         [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[faceCollection]-0-|" options:0 metrics:nil views:views]];
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[pageControl]-0-|" options:0 metrics:metrics views:views]];
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[send(sendW)]-5-|" options:0 metrics:metrics views:views]];
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[faceCollection]-paddingV-[pageControl(pageControlH)]-paddingV-[send(sendH)]-5-|" options:0 metrics:metrics views:views]];
        
    }
    return self;
}
-(CGSize)intrinsicContentSize{
    return CGSizeMake(UIViewNoIntrinsicMetric, 150);
}

+(instancetype)emojiView{
    return [[self alloc] init];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.page;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 1;
    
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    mPageControl.currentPage = (scrollView.contentOffset.x)/scrollView.bounds.size.width;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    EmojiCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.datas = [self.data subarrayWithRange:NSMakeRange(indexPath.section * self.pageCount, MIN(self.pageCount, self.data.count -indexPath.section * self.pageCount))];
    cell.delegate = self.delegate;
    return cell;
}
-(void)sendContentAction:(UIButton *)sender{
    if([self.delegate respondsToSelector:@selector(emojiViewDidSendAction)]){
        [self.delegate emojiViewDidSendAction];
    }
}
@end
